package org.kernely.core;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.kernely.core.service.KernelyBundle;
import org.kernely.core.service.KernelyBundleService;
import org.kernely.core.service.KernelyBundleServiceListener;


public class KernelyBundleServiceImpl implements KernelyBundleService {

	// List of all KernelyBundle deployed
	private List<KernelyBundle> modules = new ArrayList<KernelyBundle>();

	// List of all KernelyBundleListener registered
	private ArrayList<KernelyBundleServiceListener> listeners = new ArrayList<KernelyBundleServiceListener>();

	/**
	 * @see KernelyBundleService.registerModule
	 */
	@SuppressWarnings("unchecked")
	public final synchronized void registerModule(KernelyBundle module) {
		modules.add(module);
		// Fired the event that a new bundle has been deployed to all listeners
		for (KernelyBundleServiceListener listener : (ArrayList<KernelyBundleServiceListener>) listeners.clone()) {
			//listener.moduleRegistered(this, module);
		}
	}

	/**
	 * @see KernelyBundleService.unregisterModule
	 */
	@SuppressWarnings("unchecked")
	public final synchronized void unregisterModule(KernelyBundle module) {
		modules.remove(module);
		// Fired the event that a bundle has been undeployed to all listeners
		for (KernelyBundleServiceListener listener : (ArrayList<KernelyBundleServiceListener>) listeners.clone()) {
			listener.moduleUnregistered(this, module);
		}
	}

	/**
	 * @see KernelyBundleService.getModules
	 */
	public final List<KernelyBundle> getModules() {
		return Collections.unmodifiableList(modules);
	}

	/**
	 * @see KernelyBundleService.addListener
	 */
	public final synchronized void addListener(KernelyBundleServiceListener listener) {
		listeners.add(listener);
	}

	/**
	 * @see KernelyBundleService.removeListener
	 */
	public final synchronized void removeListener(KernelyBundleServiceListener listener) {
		listeners.remove(listener);
	}

	

}